package cn.icanci.loopstack.damc.api.groovy;

import java.util.HashMap;
import java.util.Map;

/**
 * @author icanci
 * @since 1.0 Created in 2025/05/17 11:37
 */
public class GroovyScriptClassRepository {
    public static final Map<MethodMockCacheKey, Class<?>> SCRIPT_CLASS_REPOSITORY = new HashMap<>();

    /**
     * 删除
     *
     * @param cacheKey cacheKey
     * @param script script
     */
    public static void add(MethodMockCacheKey cacheKey, String script) {
        SCRIPT_CLASS_REPOSITORY.put(cacheKey, GroovyClassLoaderHolder.compile(script));
    }

    /**
     * 删除
     * 
     * @param cacheKey cacheKey
     */
    public static void remove(MethodMockCacheKey cacheKey) {
        SCRIPT_CLASS_REPOSITORY.remove(cacheKey);
    }

    /**
     * 获取 Class
     * 
     * @param clazzName clazzName
     * @param methodName methodName
     * @param descriptor descriptor
     * @return Class<?>
     */
    public static Class<?> getScriptClass(String clazzName, String methodName, String descriptor) {
        MethodMockCacheKey cacheKey = new MethodMockCacheKey(clazzName, methodName, descriptor);
        return SCRIPT_CLASS_REPOSITORY.get(cacheKey);
    }
}
